package controller;


import service.UserService;
import dao.UserDao;
import entity.User;
import utils.GetNumber;
import utils.JdbcUtils;

import javax.xml.namespace.QName;
import java.sql.SQLException;
import java.util.Scanner;

import static service.UserService.allUser;
import static service.UserService.inquiryUser;


public class UserController {

    private UserDao userDao;
    private UserService userService;
    private User User;
    private GetNumber getNumber;
    private Scanner scanner;
    private JdbcUtils jdbcUtils;
    public UserController() {
       this.userDao = new UserDao();
       this.userService = new UserService();
       this.getNumber = new GetNumber();
       this.scanner = new Scanner(System.in);
       this.jdbcUtils = new JdbcUtils();

    }



    public void printMenu() throws SQLException {
            System.out.println("1.增加用户");
            System.out.println("2.删除用户");
            System.out.println("3.修改用户");
            System.out.println("4.查询用户");
            System.out.println("0.退出");
            System.out.println("========");
            System.out.println("请输入：");
    }
    public void query() throws SQLException {
        System.out.println("1.查询全部用户");
        System.out.println("2.姓名查询用户");
        System.out.println("0.退出");
    }
    public void query2() throws SQLException {
        System.out.println("请输入要查询的用户姓名");
        String name = scanner.nextLine();
        if(userService.inquiryUser(name))
            System.out.println("查询用户成功");
        else
            System.out.println("查询失败");
    }
    public void inquireUser() throws SQLException {
        GetNumber getNumber = new GetNumber();
        while (true) {
            query();
            int option=getNumber.getUserInputAsInt();
            switch (option) {
                case 1:
                   allUser();
                    break;
                case 2:
                    query2();
                    break;
                case 0:
                    return;
            }
        }

    }

    private void useradd() throws SQLException {
        System.out.println("添加用户姓名");
        String username = scanner.nextLine();
        if(UserDao.existsUsername(username)){
            System.out.println("用户已存在");
            return;
        }
        System.out.println("添加用户的性别");
        String sex = scanner.nextLine();
        System.out.println("用户选择的项目");
        String pname = scanner.nextLine();
        System.out.println("用户是否为会员");
        String superusers = scanner.nextLine();
        User user=new User(username,sex,pname,superusers);
         if(userService.addUser(user))
         System.out.println("添加用户成功");
       else
           System.out.println("添加失败");
    }

    public void dirUser() throws SQLException {
        System.out.println("请输入要删除的名字");
        String username = scanner.nextLine();
        if(userService.deleteUser(username))
            System.out.println("删除用户成功");
        else
            System.out.println("删除失败");
    }
    public void reviseUser() throws SQLException {
        System.out.println("请输入要修改的人");
        String username = scanner.nextLine();
        if(!UserDao.existsUsername(username)){
            System.out.println("用户不存在");
            return;
        }
        System.out.println("请输入修改的性别");
        String sex = scanner.nextLine();
        System.out.println("请输入修改的项目");
        String pname = scanner.nextLine();
        System.out.println("请输入修改是否会员");
        String superusers = scanner.nextLine();
        User user=new User(username,sex,pname,superusers);
        if (UserService.revise(user))
            System.out.println("修改成功");
        else
            System.out.println("修改失败");


    }


    public void run() throws SQLException {
        UserService userService = new UserService();
        GetNumber getNumber = new GetNumber();
        while (true) {
            printMenu();
            int option=getNumber.getUserInputAsInt();
            switch (option) {
                case 1:
                   useradd();
                    break;
                case 2:
                    dirUser();
                    break;
                case 3:
                   reviseUser();
                     break;
                case 4:
                   inquireUser();
                   break;
                case 0:
                    jdbcUtils.closeAllConnection();
                     return;
            }
        }
    }



}
